support aeson for Map
authorJoey Hess <joeyh@joeyh.name>
Wed, 10 May 2023 17:30:43 +0000 (13:30 -0400)
committerJoey Hess <joeyh@joeyh.name>
Wed, 10 May 2023 17:51:37 +0000 (13:51 -0400)
Make unused --json use it, which is better than the doubly nested lists
it was using.

Sponsored-By: the NIH-funded NICEMAN (ReproNim TR&D3) project
Command/Unused.hs
Utility/Aeson.hs

index 544f0da327884867e6a842f835f39e4d801b4e4a..37566cb677934c3dc882410dc0d3d223771d1e9b 100644 (file)
@@ -40,6 +40,7 @@ import qualified Data.Map as M
 import qualified Data.Vector as V
 import qualified Data.ByteString as S
 import qualified Data.ByteString.Char8 as S8
+import qualified Data.Text as T
 import Data.Char
 
 cmd :: Command
@@ -116,7 +117,7 @@ check fileprefix msg a c = do
                showLongNote $ UnquotedString $ msg unusedlist
        maybeAddJSONField
                ((if null fileprefix then "unused" else fileprefix) ++ "-list")
-               (V.fromList $ map (\(n,  k) -> (show n, serializeKey k)) unusedlist)
+               (M.fromList $ map (\(n,  k) -> (T.pack (show n), serializeKey k)) unusedlist)
        updateUnusedLog (toRawFilePath fileprefix) (M.fromList unusedlist)
        return $ c + length l
 
index 9e126c82dd84006e3b4bac391bdc9cc3099e9d5e..0eacbf0daa5648691ec75dc5b03e790cde4e9157 100644 (file)
@@ -2,7 +2,7 @@
  -
  - Import instead of Data.Aeson
  -
- - Copyright 2018-2019 Joey Hess <id@joeyh.name>
+ - Copyright 2018-2023 Joey Hess <id@joeyh.name>
  -
  - License: BSD-2-clause
  -}
@@ -33,6 +33,7 @@ import qualified Data.Text.Encoding as T
 import qualified Data.ByteString.Lazy as L
 import qualified Data.ByteString as S
 import qualified Data.Set
+import qualified Data.Map
 import qualified Data.Vector
 import Prelude
 
@@ -100,6 +101,11 @@ instance ToJSON' s => ToJSON' (Data.Vector.Vector s) where
 instance ToJSON' s => ToJSON' (Data.Set.Set s) where
        toJSON' = toJSON . map toJSON' . Data.Set.toList
 
+instance (ToJSON' v) => ToJSON' (Data.Map.Map T.Text v) where
+       toJSON' m = object $ map go (Data.Map.toList m)
+         where
+               go (k, v) = (textKey k, toJSON' v)
+
 instance (ToJSON' a, ToJSON a) => ToJSON' (Maybe a) where
        toJSON' (Just a) = toJSON (Just (toJSON' a))
        toJSON' v@Nothing = toJSON v